﻿using Core.Configuration.Api;
using IconYuan.Manager.Systems;
using IconYuan.Model.DTO.Account;
using IconYuan.Model.Entity.Systems;
using IconYuan.Model.Entity.Users;
using Microsoft.AspNetCore.Mvc;
using System.Reflection;

namespace IconYuan.Api.Controllers.Admins.Systems
{
    /// <summary>
    /// 后台管理员
    /// </summary>
    public class AdminController : AdminApi
    {
        readonly AdminManager adminSvc;
        public AdminController()
        {
            adminSvc = new AdminManager();
        }

        /// <summary>
        /// 手机号+密码登录
        /// </summary>
        [HttpPost]
        public async Task<Admin> Login(LoginDto model)
        {
            var admin = await adminSvc.GetAsync(x => x.name == model.name);
            if (admin == null) throw new Exception("账号不存在");
            if (!admin.pwd.Equals(model.pwd)) throw new Exception("用户名密码不正确");
            return admin;
        }
        /// <summary>
        /// 获取用户
        /// </summary>
        /// <param name="token"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<Admin> GetByToken(string token)
        {
            return await adminSvc.GetAsync<Admin>(x => x.id == Convert.ToInt32(token));
        }
    }
}
